<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <a href="http://127.0.0.1:8800/auth">授权并获取微信用户信息</a>
    <div id="user-info"></div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
	var appid= 'wxa3317c46f994138f' // appid
	var secret = 'fcd76ee250ec7ad7a548bba26f79e5dd' // 密匙appsecret
	var url = 'https://yj0703.gitee.io/test/index.html' // 回调链接地址，需转码为URI格式，必须去除#后的参数
	var scope = 'snsapi_userinfo' // snsapi_base静默授权，snsapi_userinfo弹出授权页面， snsapi_login 网页应用目前仅填写
	var state= '' // 重定向带的参数
	var code= '' // 
	//window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+encodeURIComponent(url)+'&response_type=code&scope=snsapi_base&state=#wechat_redirect'
	console.log(getQueryString('__hbt'))
	if (getQueryString('code')) {
		code = getQueryString('code')
	} else {
		window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+encodeURIComponent(url)+'&response_type=code&scope='+scope+'&state=#wechat_redirect'
	}
	// 拿code
	// 用code 换 
	var getUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+appid+'&secret='+secret+'&code='+code+'&grant_type=authorization_code'

	$.ajax({
     url:getUrl,
     dataType:'jsonp',
     processData: false, 
     type:'get',
     success:function(res){
       console.log(res);
     },
     error:function(e) {
     	console.log(r);
     }});
	var resData = {
		"access_token":"14_-_jk0CpT6JCfzB2lmCDWkibLkE5XaJGDXg9BKBxtgv3sD8mgAfL7fugS7Wtm2m_tFwaXQUBvAYP_AXD6C7YHaQ",
		"expires_in":7200,
		"refresh_token":"14_-_jk0CpT6JCfzB2lmCDWkjBM_V2jtAiO-fiF8kkjRg22KxmEQ9RQ_oA_RfTiAKiRvi2Mo4xLpNUiHjaU3U2Rqg",
		"openid":"owL8O1JDl3yxUIgWpvlxMEbV9S-k",
		"scope":"snsapi_base"}
	// 请求地址
	var baseaccess_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+ secret
	console.log( 'baseaccess_token', baseaccess_token)
	var access_token = '14_QSBfVN4KVcChx9yoGtf_pJ1ZMkK6hNz1Qc0x9w1k10D_FDtlsF2Ao2Pt00-LvTT8zn03j8cmUh4Uvr_2Tp7Gyw25_XkRz3D1j--_Uy_2wQA0ekUC8f5pHdm5zlF3mjKYMUoJlHCeTTnnuHIoVBCgACAYEF'
	var url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='+access_token +'&openid='+resData.openid+'&lang=zh_CN'
	//https://api.weixin.qq.com/sns/userinfo
	console.log( '用户信息', url)
	function getQueryString(name) { 
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
        var r = window.location.search.substr(1).match(reg); 
        if (r != null) return unescape(r[2]); 
        return null; 
   }
</script>
</html>